package cn.codesea.housework_user.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickServiceVOListener;
import cn.codesea.housework_user.entity.Service;
import cn.codesea.housework_user.entity.ServiceVO;

public class WorkerSerListRcvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    LayoutInflater mInflater;
    List<ServiceVO> mDatas;


    //声明自定义的监听接口
    private OnRecyclerItemClickServiceVOListener monItemClickListener;

    //提供set方法供Activity或Fragment调用
    public void setRecyclerItemClickListener(OnRecyclerItemClickServiceVOListener listener){
        monItemClickListener=listener;
    }


    public WorkerSerListRcvAdapter(Context context, List<ServiceVO> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = mInflater.inflate(R.layout.item_recycler_worker_serlist, parent, false);
        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {

        if(holder instanceof ItemViewHolder){

            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            ServiceVO ser = mDatas.get(position);
            itemViewHolder.mTvContent.setText(ser.getName());


            List<Service> services = mDatas.get(position).getServices();
            for (int i = 0;i<services.size();i++){
                final Service service = services.get(i);
                View smallItem = mInflater.inflate(R.layout.item_service,null);
                TextView serName_tv = smallItem.findViewById(R.id.serName_tv);
                serName_tv.setText(service.getName());

                AppCompatCheckBox checkbox = smallItem.findViewById(R.id.checkbox);
                checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked){
//                            Toast.makeText(mContext,position+" "+service.getName() + "选中",Toast.LENGTH_SHORT).show();
//                            checks.add(service.getId());
                            service.setChecked(true);
                        }else {
//                            Toast.makeText(mContext,position+" "+service.getName() + "取消选中",Toast.LENGTH_SHORT).show();
//                            Integer item = service.getId();
//                            checks.remove(item);
                            service.setChecked(false);

                        }
                    }
                });

                itemViewHolder.detail_tv.addView(smallItem);
            }

        }

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tvContent)
        TextView mTvContent;
        @BindView(R.id.cardView)
        CardView cardView;
        @BindView(R.id.detail_tv)
        LinearLayout detail_tv;


        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (monItemClickListener!=null){
                        monItemClickListener.onItemClick(getAdapterPosition(),mDatas);
                    }
                    if (detail_tv.getVisibility()==View.VISIBLE){
                        detail_tv.setVisibility(View.GONE);
                    }else {
                        detail_tv.setVisibility(View.VISIBLE);
                    }
                }
            });



        }

    }

}
